package com.yst.gateway.monitor.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yst.gateway.monitor.models.SysUsers;

public class MapUtils {

	/**
	 * json字符串转map
	 * @param str
	 * @return
	 */
//	public static Map<String, Object> format(String str) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		try {
//			System.out.println("字符串str的格式:" + str);
//			JSONObject json = JSONObject.fromObject(str);
//			Iterator<JSONObject> iter = json.keys();
//			while(iter.hasNext()) {
//				String key1 = String.valueOf(iter.next());
//				String value1 = "";
//				if(json.get(key1).toString().contains("{")) {
//					map.put(key1, value1);
//					JSONObject object = JSONObject.fromObject(json.get(key1));
//					int num = 0;
//					for(int j=0; j<object.size(); j++) {
//						if(num == object.size()) {
//							break;
//						}else {
//							Iterator<JSONObject> it = object.keys();
//							while(it.hasNext()) {
//								String key2 = String.valueOf(it.next());
//								String value2 = String.valueOf(object.get(key2));
//								map.put(key2, value2);
//								num++;
//							}
//						}
//					}
//				}else {
//					map.put(key1, json.get(key1));
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return map;
//	}
	
	
	/**
	 * 本地调试
	 * @param args
	 */
	public static void main(String args[]) {
		
//		String str = "{\"UserLoginModel\":{\"LoginName\":null,\"LoginType\":0,\"LoginPassword\":null,\"LastLoginTime\":\"0001-01-01T00:00:00\",\"UserUniqueCode\":null,\"UserName\":null,\"MobilePhone\":null,\"Email\":null,\"NickName\":null,\"RealName\":null,\"Birthday\":null,\"UserType\":0,\"Address\":null,\"Discription\":null,\"UserToken\":null,\"UserId\":null,\"HeadUrl\":null,\"Province\":null,\"City\":null,\"County\":null,\"IDType\":0,\"IDNumber\":null,\"RealCheck\":0,\"AppCode\":null,\"ACCode\":null,\"Mediumno\":null,\"Bankid\":null,\"SId\":null,\"CreatedOnUTC\":\"0001-01-01T00:00:00\",\"Creator\":null,\"IsAudit\":0,\"IsDeleted\":0,\"Modifier\":null,\"UpdatedOnUTC\":\"0001-01-01T00:00:00\",\"req_no\":null,\"charset\":null,\"format\":null,\"lang\":null,\"zone\":null,\"lo\":null,\"la\":null,\"pager\":0,\"pager_len\":0,\"query_str\":null,\"order_str\":null,\"ret_str\":null,\"ret_filter_str\":null,\"login_token\":null},\"AuthCode\":null,\"Success\":false,\"ResultCode\":\"0018\",\"Message\":\"登录名和密码不匹配\",\"req_no\":null,\"op_ret_code\":null,\"op_err_obj\":null,\"op_err_msg\":null,\"op_warn_msg\":null,\"query_str\":null,\"order_str\":null,\"ret_str\":null,\"ret_filter_str\":null,\"pager\":0,\"pager_len\":0,\"pager_max\":0}";
//		Map<String, Object> res = new MapUtils().format(str);
//		String str = "{"UserLoginModel":{"LoginName":"siliang","LoginType":3,"LoginPassword":":QjQthYY10X7YKeZNXv6XV2uj9clQS9v0:2iVcGXNZn/1rRi2/GFteJH8/mZQLMSy","LastLoginTime":"2016-02-24T14:19:06","UserUniqueCode":"8008010021674246","UserName":"siliang","MobilePhone":null,"Email":null,"NickName":"siliang","RealName":"siliang","Birthday":null,"UserType":0,"Address":null,"Discription":null,"UserToken":"79865dd9daf34fb89add01a476052c0b172O168O254O37","UserId":null,"HeadUrl":null,"Province":null,"City":null,"County":null,"IDType":0,"IDNumber":null,"RealCheck":0,"AppCode":"1001000000020198","ACCode":"1001000000000180","Mediumno":null,"Bankid":null,"SId":null,"CreatedOnUTC":"0001-01-01T00:00:00","Creator":null,"IsAudit":0,"IsDeleted":0,"Modifier":null,"UpdatedOnUTC":"0001-01-01T00:00:00","req_no":null,"charset":null,"format":null,"lang":null,"zone":null,"lo":null,"la":null,"pager":0,"pager_len":0,"query_str":null,"order_str":null,"ret_str":null,"ret_filter_str":null,"login_token":null},"AuthCode":"b69fbaa53bee4612a49ff84783a1c2fc172O168O254O37","Success":true,"ResultCode":"0000","Message":"成功","req_no":null,"op_ret_code":null,"op_err_obj":null,"op_err_msg":null,"op_warn_msg":null,"query_str":null,"order_str":null,"ret_str":null,"ret_filter_str":null,"pager":0,"pager_len":0,"pager_max":0}";
		String str = "{\"UserLoginModel\":{\"LoginName\":null,\"LoginType\":0,\"LoginPassword\":null,\"LastLoginTime\":\"0001-01-01T00:00:00\",\"UserUniqueCode\":null,\"UserName\":null,\"MobilePhone\":null,\"Email\":null,\"NickName\":null,\"RealName\":null,\"Birthday\":null,\"UserType\":0,\"Address\":null,\"Discription\":null,\"UserToken\":null,\"UserId\":null,\"HeadUrl\":null,\"Province\":null,\"City\":null,\"County\":null,\"IDType\":0,\"IDNumber\":null,\"RealCheck\":0,\"AppCode\":null,\"ACCode\":null,\"Mediumno\":null,\"Bankid\":null,\"SId\":null,\"CreatedOnUTC\":\"0001-01-01T00:00:00\",\"Creator\":null,\"IsAudit\":0,\"IsDeleted\":0,\"Modifier\":null,\"UpdatedOnUTC\":\"0001-01-01T00:00:00\",\"req_no\":null,\"charset\":null,\"format\":null,\"lang\":null,\"zone\":null,\"lo\":null,\"la\":null,\"pager\":0,\"pager_len\":0,\"query_str\":null,\"order_str\":null,\"ret_str\":null,\"ret_filter_str\":null,\"login_token\":null},\"AuthCode\":null,\"Success\":false,\"ResultCode\":\"0019\",\"Message\":\"密码输入错误，输入错误5次将锁定账号24小时\",\"req_no\":null,\"op_ret_code\":null,\"op_err_obj\":null,\"op_err_msg\":null,\"op_warn_msg\":null,\"query_str\":null,\"order_str\":null,\"ret_str\":null,\"ret_filter_str\":null,\"pager\":0,\"pager_len\":0,\"pager_max\":0}";
		try {
			Map<String, Object> map = JsonUtils.parse(str, Map.class);
			System.out.println("转换的结果:" + map.get("LoginName"));
		} catch (JsonParseException e1) {
			e1.printStackTrace();
		} catch (JsonMappingException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		
//		try {
//			ObjectMapper object = new ObjectMapper();
//			JsonNode name = object.readTree(str);
//			JsonNode UserLoginModel = name.path("UserLoginModel");
//			JsonNode LastLoginTime = UserLoginModel.path("LastLoginTime");
//			System.out.println("转换完成的结果" + LastLoginTime.textValue());
//		} catch (JsonProcessingException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	
}
